package com.liujit.upms.controller;

import cn.hutool.json.JSONArray;
import com.liujit.common.constants.ContentType;
import com.liujit.common.utils.ReqUtil;
import com.liujit.minio.domain.FileDetail;
import com.liujit.minio.domain.FileUploadResult;
import com.liujit.minio.service.BucketService;
import com.liujit.minio.service.FileService;
import com.liujit.upms.domain.query.FileQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Description 文件对象管理接口
 * @Author LiuJun
 * @Date 2021-03-26 09:15:51
 */
@RequiredArgsConstructor
@RestController
@Api(tags = "文件对象管理接口")
@RequestMapping("/file")
public class FileController {

    private final BucketService bucketService;

    private final FileService fileService;

    @ApiOperation(value = "查询列表", notes = "根据条件查询文件对象列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "query", value = "文件对象列表查询条件", dataType = "FileQuery", dataTypeClass = FileQuery.class, paramType = "query") })
    @GetMapping("/getList")
    public List<FileDetail> getList(FileQuery query) {
        return bucketService.listObjects(query.getBucketName(), query.getPrefix());
    }

    @ApiOperation(value = "上传文件对象", notes = "根据文件内容上传文件对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件对象内容", dataType = "MultipartFile", dataTypeClass = MultipartFile.class) })
    @PostMapping("/upload")
    public FileUploadResult upload(MultipartFile file) {
        FileUploadResult result = new FileUploadResult();
        // 文件上传
        try {
            result = fileService.putObject(file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @SneakyThrows
    @ApiOperation(value = "下载文件对象", notes = "根据文件对象路径下载文件对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "path", value = "文件对象路径", dataType = "String", dataTypeClass = String.class, paramType = "query") })
    @GetMapping("/download")
    public void download(String path) {
        HttpServletResponse response = ReqUtil.getResponse();
        InputStream inputStream = fileService.getObject(path);
        // 获取文件名
        String fileName = path.substring(path.lastIndexOf("/") + 1);
        // 获取文件后缀
        String suffix = fileName.substring(path.lastIndexOf("."));
        response.reset();
        response.setContentType(ContentType.getContentType(suffix));
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
        while ((len = inputStream.read(b)) > 0) {
            response.getOutputStream().write(b, 0, len);
        }
        inputStream.close();
    }

    @ApiOperation(value = "删除文件对象", notes = "根据文件路径删除文件对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "文件路径对象", dataType = "String", dataTypeClass = String.class, paramType = "body") })
    @PostMapping("/del")
    public Boolean del(@RequestBody JSONArray params) {
        if (params != null && params.size() != 0) {
            int size = params.size();
            // 删除单个文件
            for (int i = 0; i < size; i++) {
                fileService.removeObject(params.getStr(i));
            }
        }
        return Boolean.TRUE;
    }
}
